<?php
error_reporting(E_ERROR | E_PARSE);
//test_house script script created by Robert Cox on August 1st 2011
//Commandline Usage: test_admeldendtoend.php commandline (debug)
//URL Usage: http://.../../test_admeldendtoend.php?web=1              <= normal mode
//      -or- http://.../../test_admeldendtoend.php?web=1&debug=1      <= debug mode

// set start time
$mtime = microtime();
$mtime = explode(' ', $mtime);
$mtime = $mtime[1] + $mtime[0];
$starttime = $mtime;

if(isset($_GET['debug'])){
                $debug = TRUE;
                $GLOBALS["debug"] = TRUE;
        } else {
                unset($debug);
        }

if(isset($_GET['web'])){
                $web = TRUE;
                $GLOBALS["web"] = TRUE;
        } else {
                unset($web);
        }


if (isset($debug)) { 
	echo "<br>* * * * *  D E B U G = O N * * * * *<br>";
	if (isset($web)) { 
		echo "<br>* * * * *  W E B = O N  * * * * *<br>";}
}


$status = "pending";
$testname = "AdMeld End-to-End Test";
$testshortname = "admeldendtoendtest";
$testdesc = "Verify that the bid and ad selection system is working properly";
$testurl = "test_admeldendtoend.php";
$testtime = time();


$today = date("F j, Y, g:i a");
$hour = date("G");
$datestamp = date("Y-m-d");
$random = rand();
$Targeting="\"house\"";

$cookiefile = tempnam("/tmp", "CURLCOOKIE");

// setup command line args (commandline only)
if (isset($argv[1])) { $commandline = $argv[1]; unset($debug);} 

//
// Step 1: Make ad requests to the Canary Cluster with a German IP address. The data back from AdMeld should bid 0.
//
	$web_output = $web_output."<br><font face=\"Tahoma\" size=\"3\"><b>Step 1: Test Using a German IP address that returns a 0 bid.</b></font><br>"; 
	$commandline_output = $commandline_output."\nStep 1: Test Using a German IP address that returns a 0 bid.\n"; 
	
	$web_output = $web_output."<font face=\"Courier New\" size=\"3\"><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;http://crtldev.aimatch.com:60000/canary/hserver/mid=".$random."1/site=canary/geo_ip=89.16.139.96/area=admeld/size=468x60/trace=1</b><br>"; 
	$commandline_output = $commandline_output."     http://crtldev.aimatch.com:60000/canary/hserver/mid=".$random."1/site=canary/geo_ip=89.16.139.96/area=admeld/size=468x60/trace=1\n"; 
	
	$web_output = $web_output."&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-----<br>"; 
	$commandline_output = $commandline_output."     -----\n"; 
	$x=0;
	$de_test = "pass";
	$url1 = "http://crtldev.aimatch.com:60000/canary/hserver/mid=".$random."1/site=canary/geo_ip=89.16.139.96/area=admeld/size=468x60/trace=1";		//DE IP address = $0.00 bid

	
$de_search_strings = array(0 => "Tier125cents_110cpm", 1 => "Tier125cents_35cpm", 2 => "Tier125cents_10cpm", 3 => "Tier50cents_110cpm", 
                           4 => "Tier50cents_35cpm",   5 => "Tier50cents_10cpm",  6 => "Tier15cents_110cpm", 7 => "Tier15cents_35cpm",
						   8 => "Tier15cents_10cpm",   9 => "house",             10 => "house");

$de_response_strings = array( 0=> ">>>> Selected creative 49 (Tier125cents_110cpm) in flight 29 (Tier125cents 110 CPM Flight).",
						      1=> ">>>> Selected creative 50 (Tier125cents_35cpm) in flight 28 (Tier125cents 35 CPM Flight).",
						      2=> ">>>> Selected creative 51 (Tier125cents_10cpm) in flight 27 (Tier125cents 10 CPM Flight).",
						      3=> ">>>> Selected creative 43 (Tier50cents_110cpm) in flight 33 (Tier50cents 110 CPM Flight).",
						      4=> ">>>> Selected creative 44 (Tier50cents_35cpm) in flight 32 (Tier50cents 35 CPM Flight).",
						      5=> ">>>> Selected creative 45 (Tier50cents_10cpm) in flight 26 (Tier50cents 10 CPM Flight).",
						      6=> ">>>> Selected creative 46 (Tier15cents_110cpm) in flight 31 (Tier15cents 110 CPM Flight).",
						      7=> ">>>> Selected creative 47 (Tier15cents_35cpm) in flight 30 (Tier15cents 35 CPM Flight).",
						      8=> ">>>> Selected creative 48 (Tier15cents_10cpm) in flight 25 (Tier15cents 10 CPM Flight).",
						      9=> ">>>> Selected creative 1 (house) in flight 1 (House Ad).",
						     10=> ">>>> Selected creative 1 (house) in flight 1 (House Ad)." );
							 
while($x < 11){
	$tries = 0;
//	do{
		$output1 = MakeAdCall($url1,$cookiefile,$debug);
//		echo " OUTPUT: ".$output1."<br>";

		$output_array1 = MakeOutputArray($output1,$debug);
//		echo " OUTPUT_ARRAY1: <br>"; print_r($output_array1); echo "<br>";
//		$de_responses = Response($output_array1,$de_search_strings[$x],$debug);
		$de_responses = parseResponse($output_array1,">>>> Selected",$debug);
//		echo " DE_RESPONSES: ".$de_responses."<br>";
//		echo " DE_RESPONSE_STRINGS: ".$de_response_strings[$x]."<br>";
		$de_response_status[$x] = CheckResponse($de_responses,$de_response_strings[$x],$web,$debug);
//		echo " DE_RESPONSE_STATUS: ".$de_response_statuss[$x]."<br>";
		$tries++;
//		echo $tries." STATUS: ".$de_responses." - STRINGS: ".$de_response_strings[$x]."<br>";
//	} while (!(strcmp(trim($de_responses),trim($de_response_strings[$x])))||($tries == 1));
	
	$web_output = $web_output."<b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Expected Results: </b>".$de_response_strings[$x]."<br>"; 
	$commandline_output = $commandline_output."     Expected Results: ".$de_response_strings[$x]."\n"; 

	$web_output = $web_output."<b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Actual Results: </b>".$de_responses." in ".$tries." tries<br>"; 
	$commandline_output = $commandline_output."     Actual Results: ".$de_responses."\n"; 
	
	$web_output = $web_output."<b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Status: </b>".$de_response_status[$x]."<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-----<br>"; 
	$commandline_output = $commandline_output."     Status: ".$de_response_status[$x]."\n\n\n"; 
	if ($de_response_status[$x] == "fail") {
		$de_test = "fail";
	}
	$x++;
}

//
// Step 2: Make ad requests to the Canary Cluster with a UK ip address - the bid should be .25
//
	$web_output = $web_output."<br><hr><font face=\"Tahoma\" size=\"3\"><b>Step 2: Test Using a UK IP address that returns a 0.25 bid.</b></font><br>"; 
	$commandline_output = $commandline_output."\nStep 2: Test Using a UK IP address that returns a 0.25 bid.\n"; 
	
	$web_output = $web_output."<font face=\"Courier New\" size=\"3\"><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;http://crtldev.aimatch.com:60000/canary/hserver/mid=".$random.$i."2/site=canary/geo_ip=109.204.71.209/area=admeld/size=468x60/trace=1</b><br>"; 
	$commandline_output = $commandline_output."     http://crtldev.aimatch.com:60000/canary/hserver/mid=".$random.$i."2/site=canary/geo_ip=109.204.71.209/area=admeld/size=468x60/trace=1\n"; 
	
	$web_output = $web_output."&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-----<br>"; 
	$commandline_output = $commandline_output."     -----\n"; 

	$url2 = "http://crtldev.aimatch.com:60000/canary/hserver/mid=".$random."2/site=canary/geo_ip=109.204.71.209/area=admeld/size=468x60/trace=1";	//UK IP address = $0.25 bid
	$uk_test = "pass";
	$x=0;

$uk_search_strings = array(0 => "Tier125cents_110cpm", 1 => "Tier125cents_35cpm", 2 => "Tier125cents_10cpm", 3 => "Tier50cents_110cpm", 
                           4 => "Tier50cents_35cpm",   5 => "Tier50cents_10cpm",  6 => "Tier15cents_110cpm", 7 => "Tier15cents_35cpm",
						   8 => "aiMatch_adMeld_Integration_Creative",            9 => "aiMatch_adMeld_Integration_Creative",             
						   10 => "aiMatch_adMeld_Integration_Creative");

$uk_response_strings = array( 0=> ">>>> Selected creative 49 (Tier125cents_110cpm) in flight 29 (Tier125cents 110 CPM Flight).",
						      1=> ">>>> Selected creative 50 (Tier125cents_35cpm) in flight 28 (Tier125cents 35 CPM Flight).",
						      2=> ">>>> Selected creative 51 (Tier125cents_10cpm) in flight 27 (Tier125cents 10 CPM Flight).",
						      3=> ">>>> Selected creative 43 (Tier50cents_110cpm) in flight 33 (Tier50cents 110 CPM Flight).",
						      4=> ">>>> Selected creative 44 (Tier50cents_35cpm) in flight 32 (Tier50cents 35 CPM Flight).",
						      5=> ">>>> Selected creative 45 (Tier50cents_10cpm) in flight 26 (Tier50cents 10 CPM Flight).",
						      6=> ">>>> Selected creative 46 (Tier15cents_110cpm) in flight 31 (Tier15cents 110 CPM Flight).",
						      7=> ">>>> Selected creative 47 (Tier15cents_35cpm) in flight 30 (Tier15cents 35 CPM Flight).",
						      8=> ">>>> Selected creative 88 (aiMatch_adMeld_Integration_Creative_468X60) in flight 24 (aiMatch_adMeld_Integration_Flight).",
						      9=> ">>>> Selected creative 88 (aiMatch_adMeld_Integration_Creative_468X60) in flight 24 (aiMatch_adMeld_Integration_Flight).",
						     10=> ">>>> Selected creative 88 (aiMatch_adMeld_Integration_Creative_468X60) in flight 24 (aiMatch_adMeld_Integration_Flight)." );
							 
while($x < 11){
	$tries = 0;
//	do{
		$output2 = MakeAdCall($url2,$cookiefile,$debug);
		$output_array2 = MakeOutputArray($output2,$debug);
//		$uk_responses = Response($output_array2,$uk_search_strings[$x],$debug);
		$uk_responses = parseResponse($output_array2,">>>> Selected",$debug);
		$uk_response_status[$x] = CheckResponse($uk_responses,$uk_response_strings[$x],$web,$debug);
		$tries++;
//		echo $tries." STATUS: ".$uk_responses." - STRINGS: ".$uk_response_strings[$x]."<br>";
//	} while (!(strcmp(trim($uk_responses),trim($uk_response_strings[$x])))||($tries == 1));
	
	$web_output = $web_output."<b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Expected Results: </b>".$uk_response_strings[$x]."<br>"; 
	$commandline_output = $commandline_output."     Expected Results: ".$uk_response_strings[$x]."\n"; 

	$web_output = $web_output."<b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Actual Results: </b>".$uk_responses." in ".$tries." tries<br>"; 
	$commandline_output = $commandline_output."     Actual Results: ".$uk_responses."\n"; 
	
	$web_output = $web_output."<b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Status: </b>".$uk_response_status[$x]."<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-----<br>"; 
	$commandline_output = $commandline_output."     Status: ".$uk_response_status[$x]."\n\n\n"; 
	if ($uk_response_status[$x] == "fail") {
		$uk_test = "fail";
	}
	$x++;
}

//
// Step 3: Make ad requests to the Canary Cluster with a US ip address - the bid should be 1.00
//
	$web_output = $web_output."<br><hr><font face=\"Tahoma\" size=\"3\"><b>Step 3: Test Using a US IP address that returns a 1.00 bid.</b></font><br>"; 
	$commandline_output = $commandline_output."\nStep 2: Test Using a US IP address that returns a 1.00 bid.\n"; 
	
	$web_output = $web_output."<font face=\"Courier New\" size=\"3\"><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;http://crtldev.aimatch.com:60000/canary/hserver/mid=".$random."3/site=canary/geo_ip=69.60.16.202/area=admeld/size=468x60/trace=1</b><br>"; 
	$commandline_output = $commandline_output."     http://crtldev.aimatch.com:60000/canary/hserver/mid=".$random."3/site=canary/geo_ip=69.60.16.202/area=admeld/size=468x60/trace=1\n"; 
	
	$web_output = $web_output."&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-----<br>"; 
	$commandline_output = $commandline_output."     -----\n"; 

	$url3 = "http://crtldev.aimatch.com:60000/canary/hserver/mid=".$random."3/site=canary/geo_ip=69.60.16.202/area=admeld/size=468x60/trace=1";		//US IP address = $1.00 bid	$x=0;
	$us_test = "pass";
	$x=0;
	
$us_search_strings = array( 0 => "Tier125cents_110cpm", 1 => "Tier125cents_35cpm", 2 => "Tier125cents_10cpm", 3 => "Tier50cents_110cpm", 
                            4 => "aiMatch_adMeld_Integration_Creative",            5 => "aiMatch_adMeld_Integration_Creative",  
						    6 => "aiMatch_adMeld_Integration_Creative",            7 => "aiMatch_adMeld_Integration_Creative",
						    8 => "aiMatch_adMeld_Integration_Creative",            9 => "aiMatch_adMeld_Integration_Creative",             
						   10 => "aiMatch_adMeld_Integration_Creative");

$us_response_strings = array( 0=> ">>>> Selected creative 49 (Tier125cents_110cpm) in flight 29 (Tier125cents 110 CPM Flight).",
						      1=> ">>>> Selected creative 50 (Tier125cents_35cpm) in flight 28 (Tier125cents 35 CPM Flight).",
						      2=> ">>>> Selected creative 51 (Tier125cents_10cpm) in flight 27 (Tier125cents 10 CPM Flight).",
						      3=> ">>>> Selected creative 43 (Tier50cents_110cpm) in flight 33 (Tier50cents 110 CPM Flight).",
						      4=> ">>>> Selected creative 88 (aiMatch_adMeld_Integration_Creative_468X60) in flight 24 (aiMatch_adMeld_Integration_Flight).",
						      5=> ">>>> Selected creative 88 (aiMatch_adMeld_Integration_Creative_468X60) in flight 24 (aiMatch_adMeld_Integration_Flight).",
						      6=> ">>>> Selected creative 88 (aiMatch_adMeld_Integration_Creative_468X60) in flight 24 (aiMatch_adMeld_Integration_Flight).",
						      7=> ">>>> Selected creative 88 (aiMatch_adMeld_Integration_Creative_468X60) in flight 24 (aiMatch_adMeld_Integration_Flight).",
						      8=> ">>>> Selected creative 88 (aiMatch_adMeld_Integration_Creative_468X60) in flight 24 (aiMatch_adMeld_Integration_Flight).",
						      9=> ">>>> Selected creative 88 (aiMatch_adMeld_Integration_Creative_468X60) in flight 24 (aiMatch_adMeld_Integration_Flight).",
						     10=> ">>>> Selected creative 88 (aiMatch_adMeld_Integration_Creative_468X60) in flight 24 (aiMatch_adMeld_Integration_Flight)." );
							 
while($x < 11){
	$tries = 0;
//	do{
		$output3 = MakeAdCall($url3,$cookiefile,$debug);
		$output_array3 = MakeOutputArray($output3,$debug);
//		$us_responses = Response($output_array3,$us_search_strings[$x],$debug);
		$us_responses = parseResponse($output_array3,">>>> Selected",$debug);
		$us_response_status[$x] = CheckResponse($us_responses,$us_response_strings[$x],$web,$debug);
		$tries++;
//	} while (!(strcmp(trim($us_response_status[$x]),trim($us_response_strings[$x])))||($tries == 1));
	
	$web_output = $web_output."<b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Expected Results: </b>".$us_response_strings[$x]."<br>"; 
	$commandline_output = $commandline_output."     Expected Results: ".$us_response_strings[$x]."\n"; 

	$web_output = $web_output."<b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Actual Results: </b>".$us_responses." in ".$tries." tries<br>"; 
	$commandline_output = $commandline_output."     Actual Results: ".$us_responses."\n"; 
	
	$web_output = $web_output."<b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Status: </b>".$us_response_status[$x]."<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-----<br>"; 
	$commandline_output = $commandline_output."     Status: ".$us_response_status[$x]."\n\n\n"; 
	if ($us_response_status[$x] == "fail") {
		$us_test = "fail";
	}
	$x++;
}



if ($de_test=="pass" && $uk_test=="pass" && $us_test=="pass" ){
	$web_status_output = $web_status_output."<font face=\"Tahoma\" size=\"3\" color=\"green\"><b>***** SUCCESS - AdMeld end-to-end responses are valid for 0.00, 0.25 and 1.00 bids</b></font><br><br><hr>"; 
	$commandline_status_output = $commandline_status_output."\n***** SUCCESS - AdMeld end-to-end responses are valid for 0.00, 0.25 and 1.00 bids\n\n"; 
	$status = "pass"; 
}
else {
	$web_status_output = $web_status_output."<font face=\"Tahoma\" size=\"3\" color=\"red\"><b>***** FAIL - Some or All AdMeld end-to-end responses are not valid</b></font><br><br><hr>"; 
	$commandline_status_output = $commandline_status_output."\n***** FAIL - Some or All AdMeld end-to-end responses are not valid\n\n"; 
	$status = "fail"; 
}


	if (isset($web)) { 
		echo $web_status_output; 
		echo $web_output; }
	else {
		echo $commandline_status_output;
		echo $commandline_output; }

$filestatus = WriteResultsFile($status,$testshortname,$testname,$testdesc,$testurl,$testtime,$web,$commandline_status_output,$commandline_output,$output);

if (isset($debug)) { phpinfo(); }

//================= Functions =================
	
function WriteResultsFile($status,$testshortname,$testname,$testdesc,$testurl,$testtime,$web,$commandline_status_output,$commandline_output,$output){
	// Setup the archival file name for the test
	$datestamp = date("YmdGis");
	$directory = "/http/matchtest/archived_results/admeldendtoendtest/";
	if(!is_dir($directory)){
		if(!mkdir($directory, 0777)) {
			die('Failed to create directory: '.$directory);
		}
	}

	// Write out the information to the testresults.txt file.
	$lines = file('/http/matchtest/testresults.txt');
	$numitems = count($lines);

	// Loop through our array, show HTML source as HTML source; and line numbers too.

	if (isset($debug)) { echo "<br><pre>HTTP Response Output:<br><br>"; print_r($lines); echo "</pre><br>"; }

	$newfile = $lines[0];
	$i=1;
	while($i <= $numitems) {
		$pieces = explode("|",$lines[$i]);
		if($testname == $pieces[1]){
			$replaced_line = $status."|".$testname."|".$testdesc."|".$testurl."|".$testtime."\n";
			$newfile = $newfile.$replaced_line;
		}
		else{
			$newfile = $newfile.$lines[$i];
		}
		$i++;
	}

	if (isset($debug)) { echo "<br><pre>New File Output:<br><br>"; print_r($newfile); echo "</pre><br>"; }

	$fp = fopen('/http/matchtest/testresults.txt', 'w');

	fwrite($fp, $newfile);
	fclose($fp);

// Write out results of the test to a file 
	if (isset($web)) { $testfilename = $directory.$testshortname.".web.".$status.".".$datestamp.".".$testtime.".txt"; }
	else { $testfilename = $directory.$testshortname.".commandline.".$status.".".$datestamp.".".$testtime.".txt"; }

	if (!$fp1 = fopen($testfilename,'w')) {
		echo "Cannot open file ($testfilename)";
		exit;
	}

	fwrite ($fp1,$commandline_status_output."\n\n");
	fwrite ($fp1,$commandline_output."\n\n");
//	fwrite ($fp1,"\n\n".$message3."\n\n".$output."\n\n");
	fclose($fp1);

	return 0;
}

function MakeAdCall($url,$cookiefile,$debug) {
	$UserAgent="Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.16) Gecko/20110319 Firefox/3.6.16";

    if (isset($debug)) { echo "URL: ".$url."<br>"; }

    // initilize the curl call
    $ch = curl_init();
    // set useragent
    curl_setopt($ch, CURLOPT_USERAGENT, $UserAgent);
	// Read Cookies from Cookiefile
	curl_setopt ($ch, CURLOPT_COOKIEFILE, '/http/matchtest/tests/cookiefile.txt'); 
    // Set curl to use $url for http call
    curl_setopt($ch, CURLOPT_URL, $url);
    // show client headers
    curl_setopt($ch, CURLOPT_HEADER, 1);
    // have curl put X-Forwarded-For into headers
    curl_setopt($ch,CURLOPT_HTTPHEADER, $client_headers);
    // store results as the return of curl_exec
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    // Follow redirects
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE);		
    // if HTML error 400 over over, fail
    curl_setopt($ch, CURLOPT_FAILONERROR, TRUE);
	// Save Cookies in Cookiefile
	curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookiefile); 

    $output = curl_exec($ch);
    if (isset($debug)) { echo "HTTP Response Output:<br><pre>"; print_r($output); echo "</pre><br>"; }
    //close curl connection
    curl_close($ch);

return $output;
}


function MakeOutputArray($output,$debug){
        $output_array = preg_split('/[\n]/', $output, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
        if (isset($debug)) { echo "HTTP Response Output (array):<br><pre>"; print_r($output_array); echo "</pre><br><br>"; }

return $output_array;
}


function Response($output_array,$string,$debug) {

        $line_match = array_values(preg_grep("/$string/", $output_array));
            if (isset($GLOBALS["debug"])) { echo "DEBUG - string: <pre>".$string."</pre><br>";}
            if (isset($GLOBALS["debug"])) { echo "DEBUG - output:  <br><pre>"; print_r($output_array); echo "</pre><br>";}
            if (isset($GLOBALS["debug"])) { echo "DEBUG - line_match:<br><pre>"; print_r($line_match); echo "</pre><br>";}
        if(stripos($line_match[0], " = ")){
			$explode = explode(" = ", $line_match[0]); }
        if(stripos($line_match[0], " ")){
			$explode_a = explode(" ", trim($line_match[0]));
			if (isset($debug)) { echo "\n\nRedirect To: ".$explode_a[1]."<br>"; }
			$result = $explode_a[1]; 
		}
        if(stripos($line_match[1], ">>>>")){
			$result = $line_match[1]; }
        if(stripos($line_match[0], ">>>>")){
			$result = $line_match[0]; }
        if (isset($GLOBALS["debug"])) { echo "DEBUG - result:  ".$result."<br>";}
		return $result;
}

function CheckResponse($first_response,$response_string,$web,$debug){
    if (isset($debug)) { echo "<br>First Response: ".$first_response."<br>"; }
    if (isset($debug)) { echo "<br>Response String: ".$response_string."<br>"; }

		if(strcasecmp(trim($first_response), $response_string)==0){
			$status = "pass";
        }
        else {
			$status = "fail";			
		}
	return $status;
}

function CheckCookieFile($cookiefile,$value,$second_response,$third_response,$fourth_response,$web,$debug){
	$line = file($cookiefile);
	if (isset($debug)) { echo "Value: ".$value."<br>"; }
	if (isset($debug)) { echo "Second Response: ".$second_response."<br>"; }
	if (isset($debug)) { echo "Third Response: ".$third_response."<br>"; }
	if (isset($debug)) { echo "Fourth Response: ".$fourth_response."<br>"; }
	if (isset($debug)) { echo "Cookie File Contents (array):<br><pre>"; print_r($line); echo "</pre><br>"; }

	$admeld_pieces = explode("\t",$line[4]);
	if (isset($debug)) { echo "Cookie File AdMeld Line Contents (array):<br><pre>"; print_r($admeld_pieces); echo "</pre><br>"; }
	if (trim($admeld_pieces[0]) == ".tag.admeld.com" && trim($admeld_pieces[5]) == "meld_sess" && trim($admeld_pieces[6]) == trim($value)) {
		$admeld_status = "pass"; }
	else {
		$admeld_status = "fail"; }
	if (isset($debug)) { echo "AdMeld Status: ".$admeld_status."<br>"; }

	$aimatch_pieces = explode("\t",$line[5]);
	if (isset($debug)) { echo "Cookie File aiMatch Line Contents (array):<br><pre>"; print_r($aimatch_pieces); echo "</pre><br>"; }

	$aimatch_cookie_value = str_replace(";","",$third_response).str_replace(";","",$fourth_response);
	if (isset($debug)) { echo "aiMatch Cookie Value: ".$aimatch_cookie_value."<br>"; }
	
	if (trim($aimatch_pieces[0]) == ".aimatch.com" && trim($aimatch_pieces[5]) == "External" && trim($aimatch_pieces[6]) == trim($aimatch_cookie_value)) {
		$aimatch_status = "pass"; }
	else {
		$aimatch_status = "fail"; }
	if (isset($debug)) { echo "aiMatch Status: ".$aimatch_status."<br>"; }

	if ($aimatch_status == "pass" && $admeld_status == "pass"){
		$status = "pass"; }
	else {
		$status = "fail"; }

	if (isset($debug)) { echo "Cookie File Status: ".$aimatch_status."<br>"; }
		
return $status;
}

function parseResponse($output_array,$string,$debug) {
		$i=0;
        $num = count($output_array);
		while($i <= $num){
			if(stripos($output_array[$i],$string)){
				return $output_array[$i];
			}
		$i++;
		}
}

?>
